<?php
/**
 * 《电话区号》实现类
 *
 * @author 孙晓晔
 * @version $Id: Mobile.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/TelUtil.class.php');

class Mobile extends TelUtil {

	public $tbl = Constant::tbl_mobile;

	public $orderby = array(
		'-' => '请选择',
		'mobile' => '号段',
		'province' => '省份',
		'city' => '城市',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$province = $this->code_option('4');
		$controller->assign_by_ref('province_option', $province);

		$controller->display($request, 'tel/mobile/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$province_s = $request['province_s'];
		$mobile_s = substr($request['mobile_s'], 0, 7);

		$where = " WHERE 1=1";

		if (!empty($province_s) && $province_s <> '-') {
			$where .= " AND code = '" . $province_s . "'";
		}

		if (!empty($mobile_s)) {
			$where .= " AND mobile LIKE '" . $mobile_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row['category'] = $this->category[$row['category']];

				$row['status_t'] = $this->status[$row['status']];

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'tel/mobile/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'tel/mobile/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$mobile = $request['mobile'];
		$province = $request['province'];
		$city = $request['city'];
		$zone = $request['zone'];
		$post = $request['post'];

		$status = $request['status'];
			
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(mobile, province, city, zone, post, status, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $mobile, $province, $city, $zone, $post, $status);
		// debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'tel/mobile/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$mobile = $request['mobile'];
		$province = $request['province'];
		$city = $request['city'];
		$zone = $request['zone'];
		$post = $request['post'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET mobile = '%s', province = '%s', city = '%s', zone = '%s', post = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $mobile, $province, $city, $zone, $post, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'tel/mobile/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'tel/mobile/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function _mobile($s) {
		if (!preg_match("/^1(3|4|5|7|8)\d{5,9}$/", $s)) {
			return false;
		}
		
		if (empty($s)) {
			return false;
		}
		
		$key = sprintf("%s_mobile_%s", $this->tbl, $s);
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}
		
		$mobile = substr($s, 0, 7);
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE mobile = '%s'";
		$sql = sprintf($fmt, $this->tbl, $mobile);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$row['name'] = $row['province'] . $row['city'];
			
			if (is_object($mc) && count($row) > 0) {
				$mc->set($key, $row, 0, _strtotime("1 day"));
			}
		
			return $row;
		} else {
			$db = newdb($this->dsn_w);
			$fmt = "INSERT INTO %s(mobile, createtime) VALUES('%s', now())";
			$sql = sprintf($fmt, $this->tbl, $mobile);
			// debug($sql);
			$db->query($sql);
		}
		
		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function importPost($controller, $request) {
		$this->pretreat($controller);

		$data = $request['data'];
		// echo $data;

		$aa = preg_split("/[\\n]+/i", $data);
		// print_r($a);

		$province = '';
		$db = newdb($this->dsn);
		while (list($k, $v) = each($aa)) {
			$buf = trim($v);
			if (strlen($buf) < 5) {
				continue;
			}

			$a = preg_split("/[\t|\s]+/i", $buf);
			// print_r($a);
			if (count($a) < 2) {
				$province = $buf;
				continue;
			}

			$city = trim($a[0]);
			$zone = trim($a[1]);

			$fmt = "INSERT INTO %s(`province`, city, zone) VALUES('%s', '%s', '%s')";
			$sql = sprintf($fmt, $this->tbl, $province, $city, $zone);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 更新号码
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function showji($controller, $request) {
		$this->pretreat($controller);

		$dd = date('Y-m-d', strtotime('-7 day'));
		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s WHERE zone is null and createtime > '%s'";
		$sql = sprintf($fmt, $this->tbl, $dd);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$url = 'http://api.showji.com/Locating/default.aspx?output=json&m=' . $row['mobile'];
			$s = file_get_contents($url);
			echo $s . "\n";
			$a = json_decode($s);

			if (empty($a->Province) || empty($a->AreaCode)) {
				echo "S";
				continue;
			}

			$ret = array();
			$ret['province'] = $a->Province;
			$ret['city'] = $a->City;

			$ret['name'] = $ret['province'] . $ret['city'];
			$fmt = "UPDATE %s SET province = '%s', city = '%s', zone = '%s', post = '%s', corp = '%s', card = '%s', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $a->Province, $a->City, $a->AreaCode, $a->PostCode, $a->Corp, $a->Card, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function k780($controller, $request) {
		$this->pretreat($controller);
		
		require_once(dirname(__FILE__) . '/../info/Zone.class.php');
		$zone = new Zone();
	
		$dd = date('Y-m-d', strtotime('-7 day'));
		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s WHERE (zone is null OR city is null) and createtime > '%s'";
		$sql = sprintf($fmt, $this->tbl, $dd);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$fmt = "http://api.k780.com:88/?app=phone.get&phone=%s&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
			$url = sprintf($fmt, $row['mobile'] . '0000');
			echo $url . "\n";
			$s = file_get_contents($url);
			echo $s . "\n";
			$a = json_decode($s);
			
			if ($a->success != '1') {
				continue;
			}
			
			$r = $a->result;
			$phone = $r->phone;
			$area = $r->area;
			$att = $r->att;
			$ctype = $r->ctype;
			$par = $r->par;
	
			if (empty($area)) {
				echo "S";
				continue;
			}
			
			$a = $zone->_byzone($area);
	
			$fmt = "UPDATE %s SET zone = '%s', card = '%s', province = '%s', city = '%s', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $area, $ctype, $a['province'], $a['city'], $row['id']);
			// debug($sql);
			$db->query($sql);
		}
	
		logdate($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function cdr($controller, $request) {
		$this->pretreat($controller);

		$db = newdb('ns_sip');
		$fmt = "SELECT * FROM %s WHERE (length(src) > 9 or length(dst) > 9) AND calldate > DATE_ADD(now(), INTERVAL -1 day)";
		$sql = sprintf($fmt, Constant::tbl_cdr);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if (preg_match("/^(91|901|1|01)(3|4|5|7|8)\d{9}$/", $row['src'])) {
				echo 'src = ' . $row['src'] . "\n";
				$this->_number($row['src']);
				continue;
			}

			if (preg_match("/^(91|901|1|01)(3|4|5|7|8)\d{9}$/", $row['dst'])) {
				echo 'dst = ' . $row['dst'] . "\n";
				$this->_number($row['dst']);
				continue;
			}
		}

		logdate($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function t($controller, $request) {
		$this->pretreat($controller);

		$s = "13532789657";
		$s = "9118080085263972484";
		$s = "9966880085263972484";

		$a = $this->_number($s);
		
		print_r($a);

		logdate($controller, $request);
		return true;
	}
}
?>